package com.example.hyw.szysteam.presenter;

import android.content.Context;
import android.view.View;

import com.example.hyw.szysteam.POJO.HttpResult;
import com.example.hyw.szysteam.POJO.User;
import com.example.hyw.szysteam.POJO.UserInfo;
import com.example.hyw.szysteam.model.UserModel;
import com.example.hyw.szysteam.view.MainActivity;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by HYW on 2018/6/5.
 */

public class LoginPresenter {
    private UserModel userModel=new UserModel();
    private MainActivity mainActivity;
    private Context context;

    public LoginPresenter(MainActivity mainActivity) {
        this.mainActivity = mainActivity;
        this.context = mainActivity;
    }

    public void toLogin(final String email, final String password){
        userModel.login(email,password)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<HttpResult<User>>() {
                    @Override
                    public void accept(HttpResult<User> userHttpResult) throws Exception {
                        User user=userHttpResult.getContent();
                        if(userHttpResult.getResult()==HttpResult.RESULT_SUCCESS){
                            saveUserInfo(user,context);
                            UserInfo.setEmail(user.getEmail());
                            UserInfo.setName(user.getName());
                            UserInfo.setSchool(user.getSchool());
                            UserInfo.setSex(user.isSex());
                            UserInfo.setAge(user.getAge());
                            UserInfo.setType(user.getType());
                        }else{
                            mainActivity.loginFail();
                        }
                    }
                });
    }

    private void saveUserInfo(User user,Context context){
        userModel.saveUserInfo(user,context)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        if(aBoolean==true){
                            mainActivity.loginSuccess();
                        }else{
                            mainActivity.loginFail();
                        }
                    }
                });
    }

    public void getUserInfo(Context context){
        userModel.getUserInfo(context)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<User>() {
                    @Override
                    public void accept(User user) throws Exception {
                        if(user.getEmail()!=""){
                            userModel.login(user.getEmail(),user.getPassword())
                                    .subscribeOn(Schedulers.io())
                                    .unsubscribeOn(Schedulers.io())
                                    .observeOn(AndroidSchedulers.mainThread())
                                    .subscribe(new Consumer<HttpResult<User>>() {
                                        @Override
                                        public void accept(HttpResult<User> userHttpResult) throws Exception {
                                            User user=userHttpResult.getContent();
                                            if(userHttpResult.getResult()==HttpResult.RESULT_SUCCESS){
                                                mainActivity.nextActivity();
                                                UserInfo.setEmail(user.getEmail());
                                                UserInfo.setName(user.getName());
                                                UserInfo.setSchool(user.getSchool());
                                                UserInfo.setSex(user.isSex());
                                                UserInfo.setAge(user.getAge());
                                                UserInfo.setType(user.getType());
                                            }else{
                                                mainActivity.loginExpire();
                                            }
                                        }
                                    });
                        }
                    }
                });
    }
}
